package com.ideal.manage.esms.controller.project;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.ArithmeticDeployManage;
import com.ideal.manage.esms.bean.project.ArithmeticManage;
import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.bean.project.UserInfoDTO;
import com.ideal.manage.esms.bean.sys.Parameter;
import com.ideal.manage.esms.bean.sys.Role;
import com.ideal.manage.esms.service.project.AccountManageService;
import com.ideal.manage.esms.service.project.ArithmeticManageService;
import com.ideal.manage.esms.service.project.ProjectService;
import com.ideal.manage.esms.service.system.ParameterService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 83730 on 2018/4/17.
 */
@Controller
@RequestMapping("project/accountManage")
public class AccountManageController {

    @Resource
    private AccountManageService accountManageService;
    /**
     * @deprecated
     */
    @Resource
    private ArithmeticManageService arithmeticManageService;

    @Resource
    private ProjectService projectService;

    @RequestMapping("arithmeticDeploy_list")
    public void arithmeticDeployList(Model model) {

    }

    @RequestMapping("arithmetic_import")
    public void arithmeticImport(Model model) {

    }

    @RequestMapping("condition_info")
    public void conditionInfo(Model model) {

    }
    @RequestMapping("inParameter_info")
    public void inParameterInfo(Model model) {

    }
    @RequestMapping("outParameter_info")
    public void outParameterInfo(Model model) {

    }

    @RequestMapping("inParameter_data")
    @ResponseBody
    public PageDto inParameterData(int pageNum, HttpServletRequest request) {
        Page<ParameterInfo> page = projectService.findAllParameterInfos(pageNum,request);
        List<ParameterInfo> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    @RequestMapping("outParameter_data")
    @ResponseBody
    public PageDto outParameterData(int pageNum, HttpServletRequest request) {
        Page<ParameterInfo> page = projectService.findAllParameterInfos(pageNum,request);
        List<ParameterInfo> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    @RequestMapping("condition_data")
    @ResponseBody
    public PageDto conditionData(int pageNum, HttpServletRequest request) {
        Page<ParameterInfo> pageParameterInfo = projectService.findAllParameterInfos(pageNum,request);
        List<ParameterInfo> parameterInfos = pageParameterInfo.getContent();

        long total = pageParameterInfo.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }


    @RequestMapping("account_save")
    @ResponseBody
    public Result arithmeticImportSave(UserInfoDTO userInfoDTO, HttpServletResponse servletResponse) throws IOException {
//        arithmeticManageService.saveArithmetic(name,version,remark,filename);
        accountManageService.saveUserInfo(userInfoDTO);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    @RequestMapping("account_del")
    @ResponseBody
    public Result deployEdit(Model model,String []ids) {
        accountManageService.delUserInfo(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

    @RequestMapping("deploy_data")
    @ResponseBody
    public PageDto deployData(int pageNum, HttpServletRequest request) {
        Page<ArithmeticDeployManage> page = arithmeticManageService.findAllDeployManages(pageNum,request);
        List<ArithmeticDeployManage> arithmeticDeployManages = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(arithmeticDeployManages);
        pageDto.setTotal(total);
        return pageDto;
    }
    @RequestMapping("accountManageList")
    public void arithmeticManageList(Model model) {

    }

    @RequestMapping("account_add")
    public void manageAdd(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("account_edit")
    public void manageEdit(Integer id, Model model) {
        System.out.println(1111);
        if(null != id){
            UserInfoDTO  findAllById= accountManageService.findAllById(id);
            if(findAllById.getMenu()==null){
                findAllById.setMenu("");
            }
            model.addAttribute("userInfo",findAllById);
        }
    }

    /**
     * 账户管理数据查询列表
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("manage_data")
    @ResponseBody
    public PageDto manageData(int pageNum, HttpServletRequest request) {
//        Page<ArithmeticManage> page = arithmeticManageService.findAllManages(pageNum,request);
        Page<UserInfoDTO> page = accountManageService.findAllManages(pageNum, request);
        List<UserInfoDTO> arithmeticManages = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(arithmeticManages);
        pageDto.setTotal(total);
        return pageDto;
    }
}
